package com.cloud.mall.sms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.sms.input.SmsHomeBrandInpDTO;
import com.cloud.mall.sms.input.SmsHomeNewProductInpDTO;
import com.cloud.mall.sms.mapper.SmsHomeBrandMapper;
import com.cloud.mall.sms.mapper.SmsHomeNewProductMapper;
import com.cloud.mall.sms.model.SmsHomeBrand;
import com.cloud.mall.sms.model.SmsHomeBrandExample;
import com.cloud.mall.sms.model.SmsHomeNewProduct;
import com.cloud.mall.sms.model.SmsHomeNewProductExample;
import com.cloud.mall.sms.out.SmsHomeBrandOut;
import com.cloud.mall.sms.out.SmsHomeNewProductOut;
import com.cloud.mall.sms.service.SmsHomeBrandService;
import com.cloud.mall.sms.service.SmsHomeNewProductService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SmsHomeNewProductServiceImpl implements SmsHomeNewProductService {
    @Autowired
    private SmsHomeNewProductMapper homeNewProductMapper;

    @Transactional
    @Override
    public BaseResponse create(@RequestBody List<SmsHomeNewProductInpDTO> homeNewProductList) {
        List<SmsHomeNewProduct> brandList=MallBeanUtils.copyDoToDto(homeNewProductList, SmsHomeNewProduct.class);
        for (SmsHomeNewProduct smsHomeNewProduct : brandList) {
            smsHomeNewProduct.setRecommendStatus(1);
            smsHomeNewProduct.setSort(0);
            homeNewProductMapper.insert(smsHomeNewProduct);
        }
        return BaseResponse.setResultSuccess(brandList.size());
    }

    @Override
    public BaseResponse updateRecommendStatus(List<Long> ids,Integer recommendStatus) {
        SmsHomeNewProductExample example = new SmsHomeNewProductExample();
        example.createCriteria().andIdIn(ids);
        SmsHomeNewProduct record = new SmsHomeNewProduct();
        record.setRecommendStatus(recommendStatus);
        int count=homeNewProductMapper.updateByExampleSelective(record,example);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Transactional
    @Override
    public BaseResponse delete(@RequestBody List<Long> ids) {
        SmsHomeNewProductExample example = new SmsHomeNewProductExample();
        example.createCriteria().andIdIn(ids);
        return BaseResponse.setResultSuccess(homeNewProductMapper.deleteByExample(example));
    }

    @Override
    public BaseResponse updateSort(Long id, Integer sort) {
        SmsHomeNewProduct homeNewProduct = new SmsHomeNewProduct();
        homeNewProduct.setId(id);
        homeNewProduct.setSort(sort);
        int count= homeNewProductMapper.updateByPrimaryKeySelective(homeNewProduct);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }


    @Override
    public BaseResponse<CommonPage<SmsHomeNewProductOut>> list( String productName, Integer recommendStatus,Integer pageSize,Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        SmsHomeNewProductExample example = new SmsHomeNewProductExample();
        SmsHomeNewProductExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(productName)){
            criteria.andProductNameLike("%"+productName+"%");
        }
        if(recommendStatus!=null){
            criteria.andRecommendStatusEqualTo(recommendStatus);
        }
        example.setOrderByClause("sort desc");
        List<SmsHomeNewProduct> homeBrandList =homeNewProductMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(homeBrandList, SmsHomeNewProductOut.class)));
    }


}
